package com.jinhua.admin.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.jinhua.admin.dao.AdminUserDao;
import com.jinhua.admin.entity.AdminUserPo;
import com.jinhua.admin.service.AdminUserService;
import com.jinhua.admin.vo.AdminUserVo;
import lombok.extern.slf4j.Slf4j;
import com.jinhua.config.BeanCopierUtil;
import com.jinhua.config.IdUtil;
import com.jinhua.config.PaginationRequest;
import com.jinhua.config.PaginationResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

/**
 * 后台用户的service层
 */

@Service
@Slf4j
public class AdminUserServiceImpl implements AdminUserService {
    private final long ERROR_LONG = 0L;
    private final int ERROR_INT = 0;
    @Autowired
    private AdminUserDao adminUserDao;

    @Transactional
    @Override
    public long saveAdminUser(AdminUserVo adminUserVo) {
        if (adminUserVo == null) {
            log.warn("操作后台用户参数为空");
            return ERROR_LONG;
        }
        adminUserVo.setPassword(SaSecureUtil.sha256(adminUserVo.getPassword()));
        AdminUserPo adminUserPo = adminUserVo.copyAdminUserPo();
        if (adminUserPo.getId() == null) {
            log.info("start添加后台用户={}", adminUserPo);
            int result = adminUserDao.saveAdminUserIfNotExist(adminUserPo);
            return result != 0 ? adminUserPo.getId() : ERROR_LONG;
        } else {
            log.info("start修改后台用户={}", adminUserPo.getId());
            return adminUserDao.modAdminUser(adminUserPo);
        }
    }

    @Override
    public int delAdminUserByIds(Long... ids) {
        if (ids == null || ids.length == 0) {
            log.warn("删除后台用户,ids集合为空");
            return ERROR_INT;
        }
        log.warn("start删除Id集合={},后台用户", Arrays.asList(ids));
        return adminUserDao.delAdminUserByIds(ids);
    }

    @Override
    public AdminUserVo findAdminUserById(Long id) {
        if (id == null || id <= 0L) {
            log.warn("根据Id查找后台用户,所传Id不符合规范");
            return new AdminUserVo();
        }
        log.info("start根据Id={}查找后台用户", id);
        AdminUserPo adminUserPo = adminUserDao.findAdminUserById(id);
        return adminUserPo == null ? new AdminUserVo() : adminUserPo.copyAdminUserVo();
    }

    @Override
    public PaginationResponse<AdminUserVo> findAdminUserByAll(PaginationRequest<AdminUserVo> request) {
        PaginationResponse<AdminUserVo> response = new PaginationResponse<AdminUserVo>();
        if (request == null) {
            log.warn("根据条件查找后台用户,参数不对");
            return response;
        }
        log.info("start根据条件查找后台用户={}", request);
        List<AdminUserVo> adminUserVos = new ArrayList<>();
        try {
            List<AdminUserPo> adminUserPos = adminUserDao.findAdminUserByAll(request.getQueryCriteria().copyAdminUserPo(), request.getPageNum(), request.getPageSize());
            adminUserVos = BeanCopierUtil.copyList(adminUserPos, AdminUserPo.class, AdminUserVo.class);
        } catch (Exception e) {
            log.error("AdminUserServiceImpl.findOrderByAll error:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
        response.setDataList(adminUserVos);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());
        response.setTotalCount(IdUtil.getTotal());
        return response;
    }
}
